﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Luna小店.Utils;
using DevExpress.XtraGrid.Demos.util;
using mendian;
using Luna小店.DTO;
using DevExpress.XtraEditors.Popup;
using DevExpress.XtraGrid.Editors;
using DevExpress.XtraLayout;
using DevExpress.Utils.Win;
using Luna小店.service;

namespace Luna小店.UC.款式尺寸
{
    public partial class Form款式尺寸 : DevExpress.XtraEditors.XtraForm
    {
        private string styleId;
        private String name;
        public Form款式尺寸()
        {
            InitializeComponent();
            this.searchLookUpEdit选择款式.Properties.DataSource = StyleService.Get18Styles();
        }
        #region 选择款式
        private void searchLookUpEdit选择款式_Popup(object sender, EventArgs e)
        {
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clearBtn_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        private void searchLookUpEdit选择款式View_Click(object sender, EventArgs e)
        {
            int[] a = this.searchLookUpEdit选择款式.Properties.View.GetSelectedRows();
            if (a.Length > 0)
            {
                foreach (int rowHandle in a)
                {
                    this.styleId = this.searchLookUpEdit选择款式.Properties.View.GetRowCellValue(rowHandle, "ID").ToString();
                    this.name = this.searchLookUpEdit选择款式.Properties.View.GetRowCellValue(rowHandle, "Name").ToString();
                }
                this.gridControl尺寸.DataSource = FunctionHelper.JsonDeserialization<List<DTO尺寸>>(RestCall.httpGetMethod(URL.接口url + "/size/findByStyleId?styleId=" + this.styleId));
            }
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clearBtn_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit选择款式.ToolTip = null;
            searchLookUpEdit选择款式.EditValue = null;
        }
        private void searchLookUpEdit选择款式_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.name;
        }
        #endregion
        private void simpleButton导入_Click(object sender, EventArgs e)
        {
            DataSet result = ExcelUtil.getExcelDatas();
            if (result == null)
            {
                return;
            }
            DataTable dataTable = result.Tables[0];
            List<DTO尺寸> list = new List<DTO尺寸>();
            for (int i = 0; i < dataTable.Rows.Count; i++)
            {
                if (i != 0)
                {
                    String ITEM_CD = dataTable.Rows[i][0].ToString();
                    string ITEM_VALUE = dataTable.Rows[i][1].ToString();
                    string ITEM_NAME_CN = dataTable.Rows[i][2].ToString();
                    for (int j = 3; j < dataTable.Columns.Count; j++)
                    {
                        list.Add(new DTO尺寸(ITEM_CD, ITEM_VALUE, ITEM_NAME_CN, dataTable.Rows[i][j], this.styleId, "IGS_GROUP_SIZE-" + dataTable.Rows[0][j].ToString()));
                    }
                }
            }
            new Frm尺寸导入预览(list).ShowDialog();
        }
    }
}